package com.yihu.wlyy.web.statistic;

import com.yihu.wlyy.service.app.statistics.StatisticsService;
import com.yihu.wlyy.web.BaseController;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lyr on 2016/08/16.
 */
@Controller
@RequestMapping(value = "/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    StatisticsService statisticsService;

    /**
     * 指标按间隔统计
     *
     * @param startDate 起始日期
     * @param endDate 结束时间
     * @param interval 时间间隔
     * @param area 区域或机构
     * @param level 级别
     * @param index 指标
     * @return
     */
    @RequestMapping(value = "/interval")
    @ResponseBody
    public String indexInterval(String startDate,String endDate,int interval,String area,int level,String index){
        String tag = "";
        try{
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            if(index != null){
                for(String idx : indexes) {
                    result.put("index_" + idx,statisticsService.getDateIncrementDetail(startDate,endDate,interval,area,level,idx));
                }
            }

            return write(200,"查询成功!","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,tag +"查询失败!");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/increment")
    @ResponseBody
    public String getIndexIncrement(String startDate,String endDate,String area,int level,String index){
        try{
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getIntervalIncrement(startDate,endDate,area,level,idx));
            }

            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    /**
     * 指标截止日期累积量
     *
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/total")
    @ResponseBody
    public String getIndexTotal(String endDate,String area,int level,String index){
        try{
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getTotalAmount(endDate,area,level,idx));
            }

            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/lowlevel_increment")
    @ResponseBody
    public String getIndexLowLevelIncrement(String startDate,String endDate,String area,int level,String index,int sort){
        try{
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getLowLevelIncrementDetail(startDate,endDate,area,level,idx,sort));
            }

            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    /**
     * 指标截止日期累积量
     *
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/lowlevel_total")
    @ResponseBody
    public String getIndexLowLevelTotal(String endDate,String area,int level,String index,int sort){
        try{
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getLowLevelTotalDetail(endDate,area,level,idx,sort));
            }

            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    /**
     * 指标期间增长量
     *
     * @param startDate
     * @param endDate
     * @param area
     * @param level
     * @param index
     * @return
     */
    @RequestMapping("/leveltwo_increment")
    @ResponseBody
    public String getIndexLevelTwoIncrement(String startDate,String endDate,String area,int level,String index){
        try{
            String[] indexes = index.split(",");
            JSONObject result = new JSONObject();

            for(String idx : indexes){
                result.put("index_" + idx,statisticsService.getLevelTwoIndexIncrement(startDate,endDate,area,level,idx));
            }

            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

}
